package com.k2data.service;

import k2paltform.common.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import java.util.*;

public class AssetService {
    public static final Log LOG = LogFactory.getLog(AssetService.class);
    public static Map convertStr2Map(String compoundId) throws JSONException {
        JSONObject jsonObject = new JSONObject(compoundId);
        Iterator<String> iterator = jsonObject.keys();
        Map<String, String> compoundIdInMap = new HashMap<>();
        while(iterator.hasNext()) {
            String key = iterator.next();
            compoundIdInMap.put(key, jsonObject.getString(key));
        }
        return compoundIdInMap;
    }
    public static Map convertJson2Map(JSONObject compoundId) throws JSONException {
        Iterator<String> iterator = compoundId.keys();
        Map<String, String> compoundIdInMap = new HashMap<>();
        while(iterator.hasNext()) {
            String key = iterator.next();
            compoundIdInMap.put(key, compoundId.getString(key));
        }
        return compoundIdInMap;
    }
    public static String computeAssetSystemID(String assetCompoundId) throws JSONException {
        return computeAssetSystemID(convertStr2Map(assetCompoundId));
    }
    public static String computeAssetSystemID(JSONObject assetCompoundId) throws JSONException {
        return computeAssetSystemID(convertJson2Map(assetCompoundId));
    }
    public static String computeAssetSystemID(Map<String, String> assetCompoundId) {
        if(assetCompoundId == null || assetCompoundId.size() == 0)
            return null;
        return CompoundIdHandler.map2MD5(CompoundIdHandler.sortCompoundId(assetCompoundId));
    }
}
